IF statement in ST

Syntax
IF ... THEN
  ...
ELSIF ... THEN
  ...
ELSE
  ...
END_IF;
Meaning

Use the IF statement to specify that a group of statements is to be executed only if the associated Boolean →expression evaluates to value TRUE (or an equivalent). If the condition evaluates to value FALSE (or an equivalent), then either no statement is to be executed, or the statement group following the ELSE keyword (or the ELSIF keyword if its associated Boolean condition evaluates to  value TRUE or an equivalent) is to be executed.

Detailed explanation about IF

Part of IF statement

Explanation

IF condition-A THEN
   statement-A1;
   ... 
   statement-An;  

 

  • If the condition condition-A = TRUE, the execution continues with the statement block after THEN (of IF).
    The statement block of the IF-part may contain as many statements as you like. After processing the last statement, the execution continues after END_IF.

  • If the condition condition-A = FALSE, the execution continues with ELSIF. If there is no ELSIF, the execution continues with ELSE. If ELSE is also missing, the execution continues after END_IF.

ELSIF condition-B THEN
   statement-B1;
   ... 
   statement-Bn;  

 

  • If the condition condition-B = TRUE, the execution continues with the statement block after THEN (of ELSIF).
    The statement block of the ELSIF-part may contain as many statements as you like. After processing the last statement, the execution continues after END_IF.

  • If the condition condition-B = FALSE, the execution continues with the next ELSIF. If there is no other ELSIF, the execution continues with ELSE. If ELSE is also missing, the execution continues after END_IF.

You may skip the ELSIF-part or repeat it as often as you like. 

ELSE
statement-C1;
... 
statement-Cn;  

 

If all previous conditions = FALSE, the execution continues with the statement block behind ELSE.
The statement block of the ELSE-part may contain as many statements as you like. After processing the last statement, the execution continues after END_IF.

You may skip the ELSE-part.

END_IF;

 

This concludes the IF-statement.

 
Example
FUNCTION_BLOCK ExampleIfDocumentation
 VAR
   up : BOOL;
   count : INT;
 END_VAR
 
 IF up THEN     /* If 'up' = 'TRUE', counter counts up. */
   count := count + 1;
 ELSE
   count := count - 1;
 END_IF;
END_FUNCTION_BLOCK